package com.sanji.sjmallmodule.order;


import android.content.Context;

import com.sanji.sjmallconfig.ConfigAPI;
import com.sanji.sjmallconfig.ConfigConts;
import com.sanji.sjmallmanager.http.NoHttpCallBack;
import com.sanji.sjmallmanager.http.NoHttpListener;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import org.json.JSONArray;
import org.json.JSONObject;
/**
 * 数据请求处理
 * Created by zsf on 17-2-15.
 */

public class ShopCarPresenter {

//    private static final int WHAT_CART_OPERT = ConfigAPI.SHOP_CAR_LIST_URL.hashCode();

    private Context mCtx;

    public static Request mRequest = null;

    private ResultListener mResultListener;



    public ShopCarPresenter(Context context, ResultListener listener){
        this.mCtx = context;
        this.mResultListener = listener;
    }


    /**
     * 增加、减少、删除购物车数据
     * @param jsonData
     */
    public void shopcarOprt(int what,JSONArray jsonData){
        mRequest = NoHttp.createJsonArrayRequest(ConfigAPI.SHOP_CAR_LIST_URL, RequestMethod.POST);
        mRequest.setDefineRequestBodyForJson(jsonData.toString());
        NoHttpCallBack.getInstance().add(mCtx, what, mRequest, new NoHttpListener() {
            @Override
            public void onSuccessed(int what, Response response) {
                JSONObject json = (JSONObject) response.get();
                String status = json.optString("status");
                if (ConfigConts.STATUS_SUCCESS.equals(status)){
                    JSONArray result = json.optJSONArray("result");
                    if (result != null && result.length()>0){

                        mResultListener.onShopcarOpert(what,result);

                    }
                }

            }

            @Override
            public void onFailed(int what, Response response) {
                String error = response.getException().toString();
                mResultListener.onShopcarOpert(what,error);

            }
        }, true, true);
    }




    public interface ResultListener<T> {

        //购物车操作
        void onShopcarOpert(int what,Object obj);
    }


}
